import cn.lh.utils.ESClientUtil;
import org.elasticsearch.action.bulk.BulkRequestBuilder;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequestBuilder;
import org.elasticsearch.action.index.IndexRequestBuilder;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.update.UpdateRequestBuilder;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.index.query.*;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.sort.SortOrder;
import org.junit.Test;

import java.util.HashMap;

public class ESTest1 {
    @Test
    public void test1() {
        TransportClient client = ESClientUtil.getClient();
        HashMap<String, Object> map = new HashMap<>();
        IndexRequestBuilder indexRequestBuilder = client.prepareIndex("pethome", "pet", "1");
        map.put("id", 1L);
        map.put("name", "柯基");
        map.put("age", 8);
        map.put("sex", 1);
        System.out.println(indexRequestBuilder.setSource(map).get());
    }

    @Test
    public void test2() {
        TransportClient client = ESClientUtil.getClient();
        HashMap<String, Object> map = new HashMap<>();
        UpdateRequestBuilder updateRequestBuilder = client.prepareUpdate("pethome", "pet", "1");
        map.put("name", "柴犬");
        map.put("age", 7);
        map.put("sex", 0);
        System.out.println(updateRequestBuilder.setDoc(map).get());
    }

    @Test
    public void test3() {
        TransportClient client = ESClientUtil.getClient();
        DeleteRequestBuilder deleteRequestBuilder = client.prepareDelete("pethome", "pet", "1");
        System.out.println(deleteRequestBuilder.get());
    }

    @Test
    public void test4() {
        TransportClient client = ESClientUtil.getClient();
        SearchRequestBuilder pethomeSearch = client.prepareSearch("pethome");
        pethomeSearch.setTypes("user");
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        MatchQueryBuilder match = QueryBuilders.matchQuery("name", "我在源码");
        boolQueryBuilder.must(match);
        TermQueryBuilder term = QueryBuilders.termQuery("sex", 1);
        RangeQueryBuilder range = QueryBuilders.rangeQuery("age").gte(1).lte(12);
        boolQueryBuilder.filter(term).filter(range);
        pethomeSearch.setQuery(boolQueryBuilder);
        pethomeSearch.setFrom(2);
        pethomeSearch.setSize(2);
        pethomeSearch.addSort("age", SortOrder.DESC);
        SearchHits hits = pethomeSearch.get().getHits();
        for (SearchHit hit : hits) {
            System.out.println(hit.getSourceAsMap());
        }

    }

    @Test
    public void testBatch() throws Exception {
        // 1.获取ES连接
        TransportClient client = ESClientUtil.getClient();
        // 2.创建批量操作对象
        BulkRequestBuilder bulk = client.prepareBulk();
        // 3.批量创建文档，放入批量操作对象中
        for (int i = 2; i < 100; i++) {
            IndexRequestBuilder builder = client.prepareIndex("pethome", "user", i + "").setSource(
                    XContentFactory.jsonBuilder()
                            .startObject()
                            .field("id", i - 1)
                            .field("name", "我在源码学习Java!" + i)
                            .field("age", i - 1)
                            .field("sex", i%2)
                            .endObject()
            );
            BulkResponse result = bulk.add(builder).get();
            System.out.println(result.status());
        }
        // 4.关闭连接
        client.close();
    }
}
